const app = getApp()
var preFirstIndex = 0 //一级列表
var preSecondIndex = 0 //二级列表
var preThirdIndex = 0 //三级列表
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoading: true
  },

  /**
   * 生命周期函数--监听页面加载 
   */
  onLoad: function(options) {
    this.getIntentData(options)
  },
  /**
   * 传递数据
   */
  getIntentData(options) {
    var tradeareaOneList = JSON.parse(options.tradeareaOneList)
    var tradeareaTwoList = JSON.parse(options.tradeareaTwoList)
    var tradeareaThreeList = JSON.parse(options.tradeareaThreeList)

    preFirstIndex = options.firstIndex
    preSecondIndex = options.secondIndex
    preThirdIndex = options.thirdIndex

    tradeareaOneList = this.initData(tradeareaOneList, preFirstIndex)

    tradeareaTwoList = this.initData(tradeareaTwoList, preSecondIndex)

    tradeareaThreeList = this.initData(tradeareaThreeList, preThirdIndex)
    this.setData({
      tradeareaOneList: tradeareaOneList,
      tradeareaTwoList: tradeareaTwoList,
      tradeareaThreeList: tradeareaThreeList
    })

  },
  initData: function(list, index) {
    for (var i = 0; i < list.length; i++) {
      if (i == index) {
        list[i].isSelected = true
      } else {
        list[i].isSelected = false
      }
    }
    return list;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  /**
   * 一级列表点击事件
   */
  firstClick: function(e) {
    console.log(e)
    preSecondIndex = 0
    var index = e.currentTarget.dataset.id
    var list = this.data.tradeareaOneList
    var stoRid = list[index].stoId
    var selected = 'tradeareaOneList[' + index + '].isSelected'
    var preSelected = 'tradeareaOneList[' + preFirstIndex + '].isSelected'
    console.log(index)
    console.log(preFirstIndex)
    this.setData({
      [preSelected]: false,
      [selected]: true,
      isLoading: false
    })
    this.connectNetForData(0, stoRid)

    console.log('after: ' + this.data.tradeareaOneList)
    preFirstIndex = index
  },
  /**
   * 二级列表点击事件
   */
  secondClick: function(e) {
    preThirdIndex = 0
    var idx = e.currentTarget.dataset.id
    console.log(idx)
    var one = this.data.tradeareaOneList
    var two = this.data.tradeareaTwoList
    var three = this.data.tradeareaThreeList
    console.log('two :  ' + two)
    console.log('three : ' + three)
    var selected = 'tradeareaTwoList[' + idx + '].isSelected'
    var preSelected = 'tradeareaTwoList[' + preSecondIndex + '].isSelected'
    this.setData({
      [preSelected]: false,
      [selected]: true
    })
    if (three == null || three.length == 0) {
      var latitude = two[idx].latitude
      var longtitude = two[idx].longitude
      var add = two[idx].tradeareaTwo
      var pages = getCurrentPages()
      var curPage = pages[pages.length - 1]
      var prePage = pages[pages.length - 2]
      var loc = ''
      if (add.length > 4) {
        loc = add.substring(0, 4)
      } else {
        loc = add
      }
      prePage.setData({
        latitude: latitude,
        longitude: longtitude,
        bingPage: 1,
        searchText: add,
        messageCode: "",
        list: null,
        isGetData: false,
        iss: true,
        locationType: loc,
        tradeareaOneList: one,
        tradeareaTwoList: two,
        tradeareaThreeList: three,
        firstIndex: preFirstIndex,
        secondIndex: idx
      })
      prePage.connectNetForData()
      wx.navigateBack({
        delta: 1
      })
    } else {
      var sttRid = two[idx].sttId
      this.setData({
        isLoading: false
      })
      this.connectNetForData(1, sttRid)
    }

    preSecondIndex = idx
  },
  /**
   * 三级列表选择
   */
  thirdClick: function(e) {
    var that = this
    var index = e.currentTarget.dataset.id
    var list = this.data.tradeareaThreeList
    var selected = 'tradeareaThreeList[' + index + '].isSelected'
    var preSelected = 'tradeareaThreeList[' + preThirdIndex + '].isSelected'
    this.setData({
      [selected]: true,
      [preSelected]: false
    })

    var latitude = list[index].latitude
    var longtitude = list[index].longitude
    var add = list[index].tradeareaThree
    var pages = getCurrentPages()
    var curPage = pages[pages.length - 1]
    var prePage = pages[pages.length - 2]
    var loc = ''
    if (add.length > 4) {
      loc = add.substring(0, 4)
    } else {
      loc = add
    }
    prePage.setData({
      latitude: latitude,
      longitude: longtitude,
      bingPage: 1,
      searchText: add,
      messageCode: "",
      list: null,
      isGetData: false,
      iss: true,
      locationType: loc,
      tradeareaOneList: that.data.tradeareaOneList,
      tradeareaTwoList: that.data.tradeareaTwoList,
      tradeareaThreeList: list,
      firstIndex: preFirstIndex,
      secondIndex: preSecondIndex,
      thirdIndex: index
    })
    prePage.connectNetForData()
    wx.navigateBack({
      delta: 1
    })
    preThirdIndex = index
  },

  /**
   * 连接网络请求数据
   */
  connectNetForData(state, id) {
    var that = this
    var netUrl = '';
    switch (state) {
      case 0:
        netUrl = app.baseNet + 'tradearea/queryTradeareaTwo?stoRid=' + id
        break;
      case 1:
        netUrl = app.baseNet + 'tradearea/queryTradeareaThree?sttRid=' + id
        break;
    }
    wx.request({
      url: netUrl,
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        if (res.statusCode == 200) {
          var data = res.data
          if (data.messcode == '001') {
            var two = data.tradeareaTwoList
            if (typeof two != 'undefined') {
              var tradeareaTwoList = that.initData(two, preSecondIndex)
              that.setData({
                tradeareaTwoList: tradeareaTwoList
              })
            }
            var tradeareaThreeList = that.initData(data.tradeareaThreeList, 0)
            that.setData({
              tradeareaThreeList: tradeareaThreeList,
              isLoading: true
            })

          }
        }
      }
    })

  }
})